﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ProjectManApi.Extensions;
using BusinessEntity.CGGL;
using BusinessEntity;
using BusinessEntity.CGGLQuerry;
using Microsoft.AspNetCore.Authorization;
using Nacos.AspNetCore;
using learun.util;
using System.Net.Http;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System.IO;
using NLog;
using System.Text;
using System.Net.Http.Headers;
using System.Net;
using System.Web;
using BusinessRule;
using BusinessRepository;
using Newtonsoft.Json.Converters;

namespace ProjectManApi.Controllers
{
    public class CGGLController : MvcControllerBase
    {


        [HttpPost]
        public async Task<IActionResult> UpdateStatus(dynamic spjson)//
        {

            //module，dealUser，approveStatus，businessName，businessId
            try
            {
                _logger.Debug(JsonConvert.SerializeObject(spjson));

                //{
                //    "businessId":"1403255058317729793",
                //    "businessName":"quotaDetailServiceImpl",
                //    "opinion":"合格，通过，批钱",
                //    "instanceId":352926,
                //    "taskId":352983
                //}
                //string taskId = spjson.taskId;
                //string businessName = spjson.businessName;
                //string instanceId = spjson.instanceId;

                string moduleid = spjson.module;
                string userid = spjson.dealUser;
                string approveStatus = spjson.approveStatus;//UNCHECK, CHECKING,REJECTED,INVALID,CHECKED,HANDEL

                //operation，值：AGREE（同意），REJECT（驳回），INVALID（作废），ACTIVATION（激活），CANCEL（撤销），RESTART（消审）
                string operation = spjson.operation;
                string businessId = spjson.businessId;
                string businessName = spjson.businessName;

                string additionInfo = spjson.additionInfo;

                //additionInfo： {
                //    sdje
                //    ywid
                //    tname
                //  }


                //这里加了一个审定金额
                string sdje = "";

                try
                {
                    if (!string.IsNullOrEmpty(additionInfo))
                    {
                        var addi = additionInfo.ToObject<dynamic>();

                        sdje = addi.sdje;
                    }
                }
                catch
                {

                }


                if (string.IsNullOrEmpty(businessId))
                {
                    return Fail("业务id不能为空");
                }

                //待提交  未审核  审核中  作废  已审核  驳回
                //DRAFT  UNCHECK, CHECKING,INVALID,CHECKED
                string ztz = CGGLAudit.GetXMCGSPZT(approveStatus);

                if (operation == "RESTART")//消审以后
                {
                    ztz = "0";//未提交的状态
                }

                string strTname = businessName;

                await CGGLAudit.SetStatus(businessId, strTname, ztz, GetUserName(), userid, CommonHelper.ParseDecimal(sdje), additionInfo);

                #region 加日志
                LogEntity logEntity = new LogEntity();
                logEntity.F_CategoryId = 3;
                logEntity.F_OperateTypeId = ((int)OperationType.Audit).ToString();
                logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Audit);
                logEntity.F_OperateAccount = ContextHelper.GetItem("userName") as string;
                logEntity.F_OperateUserId = ContextHelper.GetItem("userId") as string;
                logEntity.F_Module = Request.Path;
                logEntity.F_IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                logEntity.F_ExecuteResult = -1;
                var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                logEntity.F_ExecuteResultJson = JsonConvert.SerializeObject(spjson, timeConverter);
                //添加日志
                await LogRule.Write(logEntity);
                #endregion

                return Json(new ResParameter { code = ResponseCode.success, message = "成功" });
            }
            catch (Exception ex)
            {
                return Json(new ResParameter { code = ResponseCode.fail, message = "操作失败" + ex.Message });
            }
        }


        #region "测试"
        private readonly INacosServerManager _serverManager;
        public CGGLController(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;
        }
        #endregion

        #region "字典"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetSHZT()
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetSHZT();

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZBFS()
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetZBFS();

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        #endregion

        #region "采购计划"
        /// <summary>
        /// 获取采购计划
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>

        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetCGJH([FromQuery] CGJHQuerry entity)
        {
            try
            {

                entity.UserID = GetUserId();
                //(DataTable, int) SelectTable = await BusinessRule.CGGL.GetCGJH(entity);
                //CG_CGJH
                entity.DepartID = await GetDepId("procurement_permission");

                (IEnumerable<dynamic>, int) SelectTable = await BusinessRule.CGGL.GetCGJH(entity, await GetDepId("procurement_permission"));

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.listid;
                    list.Add(id);


                    
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(entity.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.listid;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    string zt = item.zt.ToString();
                    item.spstatus = CGGLAudit.GetXMCGSPZTName(zt).ToLower();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }


                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        
         [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGJHByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGJHByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        [HttpGet]
        public async Task SaveCGJH(CG_CGJHLB jhs)
        {
            CG_CGJH jh = jhs.cgjh;
            string errormsg = "";
            if (string.IsNullOrEmpty(jh.bmdm))
            {
                errormsg += "部门代码不能为空;";
            }
            if (string.IsNullOrEmpty(jh.bmmc))
            {
                errormsg += "部门名称不能为空;";
            }
            if (string.IsNullOrEmpty(jh.jhmc))
            {
                errormsg += "计划名称不能为空;";
            }
            if (string.IsNullOrEmpty(jh.ncrq))
            {
                errormsg += "拟采日期不能为空;";
            }
            if (jh.jhje == 0)
            {
                errormsg += "计划金额不能为0;";
            }
            if (string.IsNullOrEmpty(jh.cgpm))
            {
                errormsg += "采购品目不能为空;";
            }
            if (string.IsNullOrEmpty(jh.wpmc))
            {
                errormsg += "物品名称不能为空;";
            }
            if (string.IsNullOrEmpty(jh.jldw))
            {
                errormsg += "计量单位不能为空;";
            }
            if (jh.wpdj == 0)
            {
                errormsg += "物品单价不能为0;";
            }
            if (jh.wpsl == 0)
            {
                errormsg += "物品数量不能为0;";
            }
            if (jh.ysje == 0)
            {
                errormsg += "预算金额不能为0;";

            }
            if (string.IsNullOrEmpty(jh.lrrid))
            {
                errormsg += "当前用户id不能为空;";
            }
            if (string.IsNullOrEmpty(jh.lrr))
            {
                errormsg += "当前用户名不能为空;";
            }
            if (string.IsNullOrEmpty(jh.lrrq))
            {
                errormsg += "录入日期不能为空;";
            }
            //if (string.IsNullOrEmpty(jh.zt))
            //{
            //    errormsg += "审核状态不能为空;";
            //}
            if (errormsg != "")
            {
                throw new Exception("采购计划中：" + errormsg);
            }
            //else
            //{
            //    if (string.IsNullOrEmpty(jh.kjnd))
            //    {
            //        jh.kjnd = DateTime.Now.ToString("yyyy");
            //    }
            //    if (string.IsNullOrEmpty(jh.listid))
            //    {
            //        jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (await JavaApi.UploadFileJavaAPI(jhs.cgjh.listid, jhs.filescodes, await GetICApiInfo()))
            //        {
            //            await BusinessRule.CGGL.InsertCG_JH(jh);
            //        }
            //    }
            //    else
            //    {
            //        if (await JavaApi.UploadFileJavaAPI(jhs.cgjh.listid, jhs.filescodes, await GetICApiInfo()))
            //        {
            //            await BusinessRule.CGGL.UpdateCG_JH(jh);
            //        }
            //    }
            //}
        }

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCGJH(CG_CGJHLB jhs)
        {
            try
            {
                await SaveCGJH(jhs);

                CG_CGJH cgjh = jhs.cgjh;
                FileUploadent filecodes = jhs.filescodes;

                string strope = jhs.ope;

                if (strope != "add" && strope != "edit" && strope != "del" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cgjh.listid))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CG_CGJH jhe = await TranstionRule.FindEntityByKey<CG_CGJH>(cgjh.listid);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cgjh.zt = "1";
                }


                if (string.IsNullOrEmpty(cgjh.kjnd))
                {
                    cgjh.kjnd = DateTime.Now.ToString("yyyy");
                }

                //保存附件
                await JavaApi.UploadFileJavaAPI(jhs.cgjh.listid, jhs.filescodes, await GetICApiInfo());

                if (strope == "add")
                {
                    cgjh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");

                    //cgjh.bmdm = 
                    if (strlsope != "submit")
                        cgjh.zt = "0";

                    if (strlsope == "submit")
                    {
                        if (string.IsNullOrEmpty(cgjh.kjnd))
                        {
                            cgjh.kjnd = DateTime.Now.ToString("yyyy");
                        }

                        
                        //提交审批
                        //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgjh.listid, amount = cgjh.jhje.ToString(), content = "[采购]" + cgjh.cgpm, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购计划.ToString(), cgjh.listid, filecodes.formCode, jhs.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CGGL.InsertCG_JH(cgjh);
                }
                else if(strope == "edit")
                {
                    if (strlsope == "submit")
                    {
                       
                       
                        //提交审批
                        //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgjh.listid, amount = cgjh.jhje.ToString(), content = "[采购]" + cgjh.cgpm, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购计划.ToString(), cgjh.listid, filecodes.formCode, jhs.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CGGL.UpdateCG_JH(cgjh);
                }

                return Success("成功");

            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCGJH([FromBody] Listid listid)
        {
            try
            {
                if (string.IsNullOrEmpty(listid.listid))
                {
                    return Fail("请发送对应采购计划的id");
                }
                else
                {
                    await BusinessRule.CGGL.DeleteCGJH(listid.listid);

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SubmitCGJH(CG_CGJHLB jhs)
        {
            try
            {
                await SaveCGJH(jhs);

                CG_CGJH cgjh = jhs.cgjh;
                FileUploadent filecodes = jhs.filescodes;

                cgjh.zt = "1";

                if (string.IsNullOrEmpty(cgjh.kjnd))
                {
                    cgjh.kjnd = DateTime.Now.ToString("yyyy");
                }

                //保存附件
                await JavaApi.UploadFileJavaAPI(jhs.cgjh.listid, jhs.filescodes, await GetICApiInfo());
                //提交审批
                //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                string urlView = "";
                string urlaudit = "";

                JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgjh.listid, amount = cgjh.jhje.ToString(), content = "[采购]" + cgjh.cgpm, deptName = GetUserDeptName() };
                JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购计划.ToString(), cgjh.listid, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(jhs.ToJson()), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                //if (!bsp)
                if (strInstanceId.StartsWith("err"))
                {
                    return Fail("提交失败，启动工作流异常");
                }

                if (string.IsNullOrEmpty(cgjh.listid))
                {
                    cgjh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    await BusinessRule.CGGL.InsertCG_JH(cgjh);
                }
                else
                {
                    await BusinessRule.CGGL.UpdateCG_JH(cgjh);
                }


                //await BusinessRule.CGGL.SubmitCGJH(cgjh.listid);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        
        /// <summary>
        /// 下载采购计划模板
        /// </summary>
        /// <returns>文件流</returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult DownloadCGJH()
        {

            try
            {
                string rootdir = AppContext.BaseDirectory;
                DirectoryInfo Dir = Directory.GetParent(rootdir);
                string webRootPath = Dir.Parent.Parent.FullName;
                string filePath = webRootPath + "/采购文件/FileTemp/采购计划模板.xls";
                //string filePath = HttpContext.Current.Server.MapPath("~/") + "FileRoot\\" + "ReportTemplate.xlsx";
                FileStream stream = new FileStream(filePath, FileMode.Open);
                var actionresult = new FileStreamResult(stream, new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("text/xls"));
                actionresult.FileDownloadName = "采购计划导入模板.xls";
                //Response.ContentLength = res.Length;
                return actionresult;
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
            //return response;
        }
        /// <summary>
        /// 导入采购计划
        /// </summary>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> ImportCGJH([FromForm] IFormCollection formCollection)
        {
            try
            {
                string rootdir = AppContext.BaseDirectory;
                DirectoryInfo Dir = Directory.GetParent(rootdir);
                string webRootPath = Dir.Parent.Parent.FullName;
                FormFileCollection filelist = (FormFileCollection)formCollection.Files;
                string nfilepath = "";
                foreach (IFormFile file in filelist)
                {
                    String Tpath = "/UpLoad/采购文件/";
                    string name = file.FileName;
                    string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    string FilePath = webRootPath + Tpath;

                    string type1 = Path.GetExtension(name);
                    DirectoryInfo di = new DirectoryInfo(FilePath);

                    if (!di.Exists)
                    {
                        di.Create();
                    }

                    using (FileStream fs = System.IO.File.Create(FilePath + FileName + type1))
                    {
                        // 复制文件
                        file.CopyTo(fs);
                        // 清空缓冲区数据
                        fs.Flush();
                    }
                    nfilepath = FilePath + FileName + type1;


                }
                string err = "";
                DataSet ds = APICommond.ImportExcel.GetExcelData(nfilepath, out err);
                if (err != "")
                {
                    return Fail(err);
                }
                List<ImportMsg> listmsg = new List<ImportMsg>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow row = ds.Tables[0].Rows[i];
                    if (Convert.ToString(row[0]).Trim() != "" && Convert.ToString(row[1]).Trim() != "" && Convert.ToString(row[2]).Trim() != ""
                         && Convert.ToString(row[3]).Trim() != "" && Convert.ToString(row[4]).Trim() != "" && Convert.ToString(row[5]).Trim() != ""
                          && Convert.ToString(row[6]).Trim() != "" && Convert.ToString(row[7]).Trim() != "" && Convert.ToString(row[8]).Trim() != ""
                           && Convert.ToString(row[9]).Trim() != "" && Convert.ToString(row[11]).Trim() != "" && Convert.ToString(row[12]).Trim() != ""
                            && Convert.ToString(row[13]).Trim() != "")
                    {
                        CG_CGJH cgjh = new CG_CGJH();
                        cgjh.kjnd = Convert.ToString(row[6]).Trim();
                        cgjh.jhmc = Convert.ToString(row[0]).Trim();
                        cgjh.bmdm = Convert.ToString(row[1]).Trim();
                        cgjh.bmmc = Convert.ToString(row[2]).Trim();
                        cgjh.lxdh = Convert.ToString(row[4]).Trim();
                        cgjh.ncrq = Convert.ToString(row[5]).Trim();
                        cgjh.cgpm = Convert.ToString(row[7]).Trim();
                        cgjh.wpmc = Convert.ToString(row[8]).Trim();
                        cgjh.ggxh = Convert.ToString(row[9]).Trim();
                        cgjh.jldw = Convert.ToString(row[10]).Trim();
                        cgjh.bz = Convert.ToString(row[14]).Trim();
                        decimal jhje = 0;
                        if (decimal.TryParse(row[3].ToString(), out jhje))
                        {
                            cgjh.jhje = Convert.ToDecimal(row[3]);
                        }
                        else
                        {
                            ImportMsg msg1 = new ImportMsg();
                            msg1.ID = i + 1;
                            msg1.ErrMsg = "第" + (i + 1) + "条数据导入检查未通过计划金额应该为数字。";
                            msg1.IsSuccess = "false";
                            listmsg.Add(msg1);
                            continue;
                        }
                        decimal dj = 0;
                        if (decimal.TryParse(row[11].ToString(), out dj))
                        {
                            cgjh.wpdj = Convert.ToDecimal(row[11]);
                        }
                        else
                        {
                            ImportMsg msg2 = new ImportMsg();
                            msg2.ID = i + 1;
                            msg2.ErrMsg = "第" + (i + 1) + "条数据导入检查未通过，单价应该为数字。";
                            msg2.IsSuccess = "false";
                            listmsg.Add(msg2);
                            continue;
                        }
                        decimal sl = 0;
                        if (decimal.TryParse(row[12].ToString(), out sl))
                        {
                            cgjh.wpsl = Convert.ToDecimal(row[12]);
                        }
                        else
                        {
                            ImportMsg msg3 = new ImportMsg();
                            msg3.ID = i + 1;
                            msg3.ErrMsg = "第" + (i + 1) + "条数据导入检查未通过，预算总价应该为数字。";

                            msg3.IsSuccess = "false";
                            listmsg.Add(msg3);
                            continue;
                        }
                        decimal je = 0;
                        if (decimal.TryParse(row[13].ToString(), out je))
                        {
                            cgjh.ysje = Convert.ToDecimal(row[13]);
                        }
                        else
                        {
                            ImportMsg msg4 = new ImportMsg();
                            msg4.ID = i + 1;
                            msg4.ErrMsg = "第" + (i + 1) + "条数据导入检查未通过，金额应该为数字。";

                            msg4.IsSuccess = "false";
                            listmsg.Add(msg4);
                            continue;
                        }
                        cgjh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        await BusinessRule.CGGL.InsertCG_JH(cgjh);
                        ImportMsg msg = new ImportMsg();
                        msg.ID = i + 1;
                        msg.ErrMsg = "";

                        msg.IsSuccess = "true";
                        listmsg.Add(msg);
                    }
                    else
                    {
                        ImportMsg msg = new ImportMsg();
                        msg.ID = i + 1;
                        msg.ErrMsg = "第" + (i + 1) + "条数据导入检查未通过，请检查带“*”的列是否为空。";

                        msg.IsSuccess = "false";
                        listmsg.Add(msg);

                    }
                }
                return Success(listmsg);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion

        #region "采购项目"
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetCGXM([FromQuery] CGXMQuerry entity)
        {
            try
            {
                entity.UserID = GetUserId();
                //(DataTable, int) SelectTable = await BusinessRule.CGGL.GetCGXM(entity);
                (IEnumerable<dynamic>, int) SelectTable = await BusinessRule.CGGL.GetCGXM(entity, await GetDepId("procurement_permission"));

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.listid;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(entity.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.listid;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    string zt = item.zt.ToString();
                    item.spstatus = CGGLAudit.GetXMCGSPZTName(zt);

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGXMByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGXMByID(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGXMMXByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGXMMXByID(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGXMJHByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGXMJHByID(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGXMZBByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGXMZBByID(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGJHLBbyids(string[] listid)
        {
            try
            {
                string uid = GetUserId();
                DataTable SelectTable = await BusinessRule.CGGL.GetCGJHLBbyids(uid, listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetCGJHLB(string kjnd, int Size, int Current, string sortString, string sort)
        {
            try
            {

                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetCGJHLB(kjnd, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetZBLB(string bmdm, string kjnd, int Size, int Current, string sortString, string sort)
        {
            try
            {
                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetCGZBLB(bmdm, kjnd, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGZBbyids(string[] listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGZBbyids(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        //采购项目保存
        [HttpGet]
        public async Task SaveCGXM(CG_CGXM cg)
        {
            string cgmx = "";
            string cgjh = "";
            string cgzb = "";
            CG_CGXMML cgxmml = cg.cgxmml;
            List<CG_CGXMMX> mxs = cg.cgxmmx;
            List<CG_CGXM_JH> jhs = cg.cgxmjh;
            List<CG_CGXMZB> zbs = cg.cgxmzb;
            string errormsg = "";
            string errormsgmx = "";
            string errormsgjh = "";
            #region 判断
            if (string.IsNullOrEmpty(cgxmml.bmdm))
            {
                errormsg += "部门代码不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bmmc))
            {
                errormsg += "部门名称不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.xmmc))
            {
                errormsg += "项目名称不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.xmqdrq))
            {
                errormsg += "项目启动日期不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.cgfs))
            {
                errormsg += "采购方式不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.cgly))
            {
                errormsg += "采购理由不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.zbfs))
            {
                errormsg += "招标方式不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.zjjg))
            {
                errormsg += "中介机构不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.zjly))
            {
                errormsg += "资金来源不能为空;";
            }
            if (cgxmml.je == 0)
            {
                errormsg += "金额不能为0;";
            }
            if (string.IsNullOrEmpty(cgxmml.zjlyfs))
            {
                errormsg += "专家来源方式不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.zzfs))
            {
                errormsg += "组织方式不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.zjly))
            {
                errormsg += "资金来源不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.zjly))
            {
                errormsg += "资金来源不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.lrrid))
            {
                errormsg += "当前用户id不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.lrr))
            {
                errormsg += "当前用户名不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.lrrq))
            {
                errormsg += "录入日期不能为空;";
            }
            //if (string.IsNullOrEmpty(cgxmml.zt))
            //{
            //    errormsg += "审核状态不能为空;";
            //}
            if (errormsg != "")
            {
                errormsg = "采购项目目录中：" + errormsg;
            }
            for (int i = 0; i < mxs.Count; i++)
            {
                if (string.IsNullOrEmpty(mxs[i].cgpm))
                {
                    errormsgmx += "采购品目不能为空;";
                }
                if (string.IsNullOrEmpty(mxs[i].wpmc))
                {
                    errormsgmx += "物品名称不能为空;";
                }
                if (string.IsNullOrEmpty(mxs[i].jldw))
                {
                    errormsgmx += "计量单位不能为空;";
                }
                if (mxs[i].xmsl == 0)
                {
                    errormsgmx += "项目数量不能为0;";
                }
                if (mxs[i].xmwpdj == 0)
                {
                    errormsgmx += "项目物品单价不能为0;";
                }
                if (mxs[i].xmje == 0)
                {
                    errormsgmx += "项目金额不能为0;";
                }
            }
            if (errormsgmx != "")
            {
                errormsgmx = "采购项目明细中：" + errormsgmx;
            }
            for (int i = 0; i < jhs.Count; i++)
            {
                if (string.IsNullOrEmpty(jhs[i].jhid))
                {
                    errormsgjh += "计划id不能为空;";
                }
                //if (jhs[i].bcsyje == 0)
                //{
                //    errormsgjh += "本次使用金额不能为0;";
                //}
            }
            if (errormsgjh != "")
            {
                errormsgjh = "采购项目计划中：" + errormsgjh;
            }
            if (errormsg + errormsgjh + errormsgmx != "")
            {
                throw new Exception(errormsg + errormsgmx + errormsgjh);
            }
            #endregion

            #region 修改
            //if (string.IsNullOrEmpty(cgxmml.listid))
            //{
            //    if (string.IsNullOrEmpty(cgxmml.kjnd))
            //    {
            //        cgxmml.kjnd = DateTime.Now.ToString("yyyy");
            //    }
            //    if (string.IsNullOrEmpty(cgxmml.xmdm))
            //    {
            //        string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
            //        DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            //        cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
            //    }
            //    cgxmml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //    if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.InsertCGXMML(cgxmml);
            //    }
            //}
            //else
            //{
            //    if (string.IsNullOrEmpty(cgxmml.kjnd))
            //    {
            //        cgxmml.kjnd = DateTime.Now.ToString("yyyy");
            //    }
            //    if (string.IsNullOrEmpty(cgxmml.xmdm))
            //    {
            //        string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
            //        DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            //        cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
            //    }
            //    if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.UpdateCGXMML(cgxmml);
            //    }
            //}
            //foreach (CG_CGXMMX mx in cg.cgxmmx)
            //{
            //    if (string.IsNullOrEmpty(mx.listid))
            //    {
            //        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (string.IsNullOrEmpty(mx.xmid))
            //        {
            //            mx.xmid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.InsertCGXMNR(mx);
            //        cgmx += "'" + mx.listid + "',";
            //    }
            //    else
            //    {
            //        if (string.IsNullOrEmpty(mx.xmid))
            //        {
            //            mx.xmid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.UpdateCGXMNR(mx);
            //        cgmx += "'" + mx.listid + "',";
            //    }
            //}
            //foreach (CG_CGXM_JH jh in cg.cgxmjh)
            //{
            //    if (string.IsNullOrEmpty(jh.listid))
            //    {
            //        jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (string.IsNullOrEmpty(jh.xmid))
            //        {
            //            jh.xmid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.InsertCGXM_JH(jh);
            //        cgjh += "'" + jh.listid + "',";
            //    }
            //    else
            //    {
            //        if (string.IsNullOrEmpty(jh.xmid))
            //        {
            //            jh.xmid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.UpdateCGXM_JH(jh);
            //        cgjh += "'" + jh.listid + "',";
            //    }
            //}
            //foreach (CG_CGXMZB zb in cg.cgxmzb)
            //{
            //    if (string.IsNullOrEmpty(zb.listid))
            //    {
            //        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (string.IsNullOrEmpty(zb.xmid))
            //        {
            //            zb.xmid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.InsertCGXM_ZB(zb);
            //        cgzb += "'" + zb.listid + "',";
            //    }
            //    else
            //    {
            //        if (string.IsNullOrEmpty(zb.xmid))
            //        {
            //            zb.xmid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.UpdateCGXM_ZB(zb);
            //        cgzb += "'" + zb.listid + "',";
            //    }
            //}
            //if (cgmx != "")
            //{
            //    cgmx = "(" + cgmx.Substring(0, cgmx.LastIndexOf(",")) + ")";
            //    await BusinessRule.CGGL.DeleteCGXMNR(cgmx, cgxmml.listid);
            //}
            //if (cgjh != "")
            //{
            //    cgjh = "(" + cgjh.Substring(0, cgjh.LastIndexOf(",")) + ")";
            //    await BusinessRule.CGGL.DeleteCGXM_JH(cgjh, cgxmml.listid);
            //}
            //if (cgzb != "")
            //{
            //    cgzb = "(" + cgzb.Substring(0, cgzb.LastIndexOf(",")) + ")";
            //    await BusinessRule.CGGL.DeleteCGXM_ZB(cgzb, cgxmml.listid);
            //}
            #endregion
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertCGXM(CG_CGXM cg)
        {
            try
            {
                //保存采购项目
                await SaveCGXM(cg);

                string cgmx = "";
                string cgjh = "";
                string cgzb = "";
               
                CG_CGXMML cgxmml = cg.cgxmml;
                List<CG_CGXMMX> mxs = cg.cgxmmx;
                List<CG_CGXM_JH> jhs = cg.cgxmjh;
                List<CG_CGXMZB> zbs = cg.cgxmzb;

                FileUploadent filecodes = cg.filescodes;

                string strope = cg.ope;

                if (strope != "add" && strope != "edit" && strope != "del" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cgxmml.listid))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CG_CGXMML jhe = await TranstionRule.FindEntityByKey<CG_CGXMML>(cgxmml.listid);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cgxmml.zt = "1";
                }

     
              

                if (string.IsNullOrEmpty(cgxmml.listid))
                {
                    if (string.IsNullOrEmpty(cgxmml.kjnd))
                    {
                        cgxmml.kjnd = DateTime.Now.ToString("yyyy");
                    }
                    if (string.IsNullOrEmpty(cgxmml.xmdm))
                    {
                        string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
                        DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
                        cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
                    }
                    cgxmml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");

                    if (strlsope != "submit")
                        cgxmml.zt = "0";

                    await JavaApi.UploadFileJavaAPI(cgxmml.listid, filecodes, await GetICApiInfo());

                    if (strlsope == "submit")
                    {
                        //FileUploadent filecodes = cg.filescodes;

                        //这里先绑定附件
                        await JavaApi.UploadFileJavaAPI(cgxmml.listid, filecodes, await GetICApiInfo());

                        //提交审批
                        //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgxmml.xmdm, amount = cgxmml.je.ToString(), content = "[采购]" + cgxmml.xmmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购项目.ToString(), cgxmml.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }

                        cgxmml.zt = "1";
                    }

                    await BusinessRule.CGGL.InsertCGXMML(cgxmml);
                }
                else
                {
                    if (string.IsNullOrEmpty(cgxmml.kjnd))
                    {
                        cgxmml.kjnd = DateTime.Now.ToString("yyyy");
                    }
                    if (string.IsNullOrEmpty(cgxmml.xmdm))
                    {
                        string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
                        DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
                        cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
                    }

                    //附件
                    await JavaApi.UploadFileJavaAPI(cgxmml.listid, filecodes, await GetICApiInfo());

                    if (strlsope == "submit")
                    {
                        //FileUploadent filecodes = cg.filescodes;

                        //这里先绑定附件
                        await JavaApi.UploadFileJavaAPI(cgxmml.listid, filecodes, await GetICApiInfo());

                        //提交审批
                        //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgxmml.xmdm, amount = cgxmml.je.ToString(), content = "[采购]" + cgxmml.xmmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购项目.ToString(), cgxmml.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CGGL.UpdateCGXMML(cgxmml);
                }
                foreach (CG_CGXMMX mx in cg.cgxmmx)
                {
                    if (string.IsNullOrEmpty(mx.listid))
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(mx.xmid))
                        {
                            mx.xmid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.InsertCGXMNR(mx);
                        cgmx += "'" + mx.listid + "',";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(mx.xmid))
                        {
                            mx.xmid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.UpdateCGXMNR(mx);
                        cgmx += "'" + mx.listid + "',";
                    }
                }
                foreach (CG_CGXM_JH jh in cg.cgxmjh)
                {
                    if (string.IsNullOrEmpty(jh.listid))
                    {
                        jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(jh.xmid))
                        {
                            jh.xmid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.InsertCGXM_JH(jh);
                        cgjh += "'" + jh.listid + "',";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(jh.xmid))
                        {
                            jh.xmid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.UpdateCGXM_JH(jh);
                        cgjh += "'" + jh.listid + "',";
                    }
                }
                foreach (CG_CGXMZB zb in cg.cgxmzb)
                {
                    if (string.IsNullOrEmpty(zb.listid))
                    {
                        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(zb.xmid))
                        {
                            zb.xmid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.InsertCGXM_ZB(zb);
                        cgzb += "'" + zb.listid + "',";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(zb.xmid))
                        {
                            zb.xmid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.UpdateCGXM_ZB(zb);
                        cgzb += "'" + zb.listid + "',";
                    }
                }
                if (cgmx != "")
                {
                    cgmx = "(" + cgmx.Substring(0, cgmx.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteCGXMNR(cgmx, cgxmml.listid);
                }
                if (cgjh != "")
                {
                    cgjh = "(" + cgjh.Substring(0, cgjh.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteCGXM_JH(cgjh, cgxmml.listid);
                }
                if (cgzb != "")
                {
                    cgzb = "(" + cgzb.Substring(0, cgzb.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteCGXM_ZB(cgzb, cgxmml.listid);
                }

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertCGXM(CG_CGXM cg)
        //{
        //    try
        //    {
        //        //保存采购项目
        //        await SaveCGXM(cg);

        //        string cgmx = "";
        //        string cgjh = "";
        //        string cgzb = "";
        //        CG_CGXMML cgxmml = cg.cgxmml;
        //        List<CG_CGXMMX> mxs = cg.cgxmmx;
        //        List<CG_CGXM_JH> jhs = cg.cgxmjh;
        //        List<CG_CGXMZB> zbs = cg.cgxmzb;

        //        if (string.IsNullOrEmpty(cgxmml.listid))
        //        {
        //            if (string.IsNullOrEmpty(cgxmml.kjnd))
        //            {
        //                cgxmml.kjnd = DateTime.Now.ToString("yyyy");
        //            }
        //            if (string.IsNullOrEmpty(cgxmml.xmdm))
        //            {
        //                string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
        //                DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        //                cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
        //            }
        //            cgxmml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //            if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.InsertCGXMML(cgxmml);
        //            }
        //        }
        //        else
        //        {
        //            if (string.IsNullOrEmpty(cgxmml.kjnd))
        //            {
        //                cgxmml.kjnd = DateTime.Now.ToString("yyyy");
        //            }
        //            if (string.IsNullOrEmpty(cgxmml.xmdm))
        //            {
        //                string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
        //                DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        //                cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
        //            }
        //            if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.UpdateCGXMML(cgxmml);
        //            }
        //        }
        //        foreach (CG_CGXMMX mx in cg.cgxmmx)
        //        {
        //            if (string.IsNullOrEmpty(mx.listid))
        //            {
        //                mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (string.IsNullOrEmpty(mx.xmid))
        //                {
        //                    mx.xmid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.InsertCGXMNR(mx);
        //                cgmx += "'" + mx.listid + "',";
        //            }
        //            else
        //            {
        //                if (string.IsNullOrEmpty(mx.xmid))
        //                {
        //                    mx.xmid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateCGXMNR(mx);
        //                cgmx += "'" + mx.listid + "',";
        //            }
        //        }
        //        foreach (CG_CGXM_JH jh in cg.cgxmjh)
        //        {
        //            if (string.IsNullOrEmpty(jh.listid))
        //            {
        //                jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (string.IsNullOrEmpty(jh.xmid))
        //                {
        //                    jh.xmid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.InsertCGXM_JH(jh);
        //                cgjh += "'" + jh.listid + "',";
        //            }
        //            else
        //            {
        //                if (string.IsNullOrEmpty(jh.xmid))
        //                {
        //                    jh.xmid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateCGXM_JH(jh);
        //                cgjh += "'" + jh.listid + "',";
        //            }
        //        }
        //        foreach (CG_CGXMZB zb in cg.cgxmzb)
        //        {
        //            if (string.IsNullOrEmpty(zb.listid))
        //            {
        //                zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (string.IsNullOrEmpty(zb.xmid))
        //                {
        //                    zb.xmid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.InsertCGXM_ZB(zb);
        //                cgzb += "'" + zb.listid + "',";
        //            }
        //            else
        //            {
        //                if (string.IsNullOrEmpty(zb.xmid))
        //                {
        //                    zb.xmid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateCGXM_ZB(zb);
        //                cgzb += "'" + zb.listid + "',";
        //            }
        //        }
        //        if (cgmx != "")
        //        {
        //            cgmx = "(" + cgmx.Substring(0, cgmx.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteCGXMNR(cgmx, cgxmml.listid);
        //        }
        //        if (cgjh != "")
        //        {
        //            cgjh = "(" + cgjh.Substring(0, cgjh.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteCGXM_JH(cgjh, cgxmml.listid);
        //        }
        //        if (cgzb != "")
        //        {
        //            cgzb = "(" + cgzb.Substring(0, cgzb.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteCGXM_ZB(cgzb, cgxmml.listid);
        //        }

        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail(ex.Message);
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCGXM([FromBody] Listid listid)
        {
            try
            {
                if (string.IsNullOrEmpty(listid.listid))
                {
                    return Fail("请发送对应采购项目的id");
                }
                else
                {
                    await BusinessRule.CGGL.DeleteCGXMML(listid.listid);

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SubmitCGXM( CG_CGXM cg)//[FromBody]
        {
            try
            {
                {

                    //保存采购项目
                    await SaveCGXM(cg);

                    string cgmx = "";
                    string cgjh = "";
                    string cgzb = "";
                    CG_CGXMML cgxmml = cg.cgxmml;
                    List<CG_CGXMMX> mxs = cg.cgxmmx;
                    List<CG_CGXM_JH> jhs = cg.cgxmjh;
                    List<CG_CGXMZB> zbs = cg.cgxmzb;

                    FileUploadent filecodes = cg.filescodes;

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cgxmml.listid, filecodes, await GetICApiInfo());

                    //提交审批
                    //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                    string urlView = "";
                    string urlaudit = "";

                    JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgxmml.xmdm, amount = cgxmml.je.ToString(), content = "[采购]" + cgxmml.xmmc, deptName = GetUserDeptName() };
                    JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购项目.ToString(), cgxmml.listid, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cg.ToJson()), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                    //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                    string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                    //if (!bsp)
                    if (strInstanceId.StartsWith("err"))
                    {
                        return Fail("提交失败，启动工作流异常");
                    }


                    //给一个提交状态
                    cgxmml.zt = "1";

                    if (string.IsNullOrEmpty(cgxmml.listid))
                    {
                        if (string.IsNullOrEmpty(cgxmml.kjnd))
                        {
                            cgxmml.kjnd = DateTime.Now.ToString("yyyy");
                        }
                        if (string.IsNullOrEmpty(cgxmml.xmdm))
                        {
                            string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
                            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
                            cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
                        }
                        cgxmml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        await BusinessRule.CGGL.InsertCGXMML(cgxmml);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(cgxmml.kjnd))
                        {
                            cgxmml.kjnd = DateTime.Now.ToString("yyyy");
                        }
                        if (string.IsNullOrEmpty(cgxmml.xmdm))
                        {
                            string sql = "SELECT 'X'+datename(year,getdate())+right('0000'+convert(varchar(20),isnull(max(convert(int,right(xmdm,5))),0)+1),5) xmdm from cg_cgxmml where kjnd=datename(year,getdate())";
                            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
                            cgxmml.xmdm = table.Rows[0]["xmdm"].ToString();
                        }
                        await BusinessRule.CGGL.UpdateCGXMML(cgxmml);
                    }
                    foreach (CG_CGXMMX mx in cg.cgxmmx)
                    {
                        if (string.IsNullOrEmpty(mx.listid))
                        {
                            mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                            if (string.IsNullOrEmpty(mx.xmid))
                            {
                                mx.xmid = cgxmml.listid;
                            }
                            await BusinessRule.CGGL.InsertCGXMNR(mx);
                            cgmx += "'" + mx.listid + "',";
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(mx.xmid))
                            {
                                mx.xmid = cgxmml.listid;
                            }
                            await BusinessRule.CGGL.UpdateCGXMNR(mx);
                            cgmx += "'" + mx.listid + "',";
                        }
                    }
                    foreach (CG_CGXM_JH jh in cg.cgxmjh)
                    {
                        if (string.IsNullOrEmpty(jh.listid))
                        {
                            jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                            if (string.IsNullOrEmpty(jh.xmid))
                            {
                                jh.xmid = cgxmml.listid;
                            }
                            await BusinessRule.CGGL.InsertCGXM_JH(jh);
                            cgjh += "'" + jh.listid + "',";
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(jh.xmid))
                            {
                                jh.xmid = cgxmml.listid;
                            }
                            await BusinessRule.CGGL.UpdateCGXM_JH(jh);
                            cgjh += "'" + jh.listid + "',";
                        }
                    }
                    foreach (CG_CGXMZB zb in cg.cgxmzb)
                    {
                        if (string.IsNullOrEmpty(zb.listid))
                        {
                            zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                            if (string.IsNullOrEmpty(zb.xmid))
                            {
                                zb.xmid = cgxmml.listid;
                            }
                            await BusinessRule.CGGL.InsertCGXM_ZB(zb);
                            cgzb += "'" + zb.listid + "',";
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(zb.xmid))
                            {
                                zb.xmid = cgxmml.listid;
                            }
                            await BusinessRule.CGGL.UpdateCGXM_ZB(zb);
                            cgzb += "'" + zb.listid + "',";
                        }
                    }
                    if (cgmx != "")
                    {
                        cgmx = "(" + cgmx.Substring(0, cgmx.LastIndexOf(",")) + ")";
                        await BusinessRule.CGGL.DeleteCGXMNR(cgmx, cgxmml.listid);
                    }
                    if (cgjh != "")
                    {
                        cgjh = "(" + cgjh.Substring(0, cgjh.LastIndexOf(",")) + ")";
                        await BusinessRule.CGGL.DeleteCGXM_JH(cgjh, cgxmml.listid);
                    }
                    if (cgzb != "")
                    {
                        cgzb = "(" + cgzb.Substring(0, cgzb.LastIndexOf(",")) + ")";
                        await BusinessRule.CGGL.DeleteCGXM_ZB(cgzb, cgxmml.listid);
                    }

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "标段信息"
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetBDXX([FromQuery] CGBDQuerry entity)
        {
            try
            {
                entity.UserID = GetUserId();
                (IEnumerable<dynamic>, int) SelectTable = await BusinessRule.CGGL.GetBDXX(entity, await GetDepId("procurement_permission"));

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.listid;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(entity.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.listid;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    string zt = item.zt.ToString();
                    item.spstatus = CGGLAudit.GetXMCGSPZTName(zt);

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);

            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetCGXMLB(string kjnd, int Size, int Current, string sortString, string sort)
        {
            try
            {

                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetCGXMLB(kjnd, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGXMbyids(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGXMbyids(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGXMMXbyids(string[] listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGXMMXbyids(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetCGXMMXLB(string listid, int Size, int Current, string sortString, string sort)
        {
            try
            {

                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetCGXMMXLB(listid, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        ///////
        [HttpGet]
        public async Task SaveBDXX(CG_BDXX cg)
        {
            string bdmx = "";
            CG_BDXXML bdml = cg.cgbdml;
            List<CG_BDXXMX> mxs = cg.cgbdmx;
            string errormsg = "";
            string errormsgmx = "";
            if (string.IsNullOrEmpty(bdml.bmdm))
            {
                errormsg += "部门代码不能为空;";
            }
            if (string.IsNullOrEmpty(bdml.bmmc))
            {
                errormsg += "部门名称不能为空;";
            }
            if (string.IsNullOrEmpty(bdml.xmmc))
            {
                errormsg += "采购项目不能为空;";
            }
            if (string.IsNullOrEmpty(bdml.cgfs))
            {
                errormsg += "采购方式不能为空;";
            }
            //if (string.IsNullOrEmpty(bdml.cgbh))
            //{
            //    errormsg += "采购编号不能为空;";
            //}
            if (bdml.xmje == 0)
            {
                errormsg += "项目金额不能为0;";
            }
            //if (string.IsNullOrEmpty(bdml.bddm))
            //{
            //    errormsg += "标段代码不能为空;";
            //}
            if (string.IsNullOrEmpty(bdml.bdmc))
            {
                errormsg += "标段名称不能为空;";
            }
            if (string.IsNullOrEmpty(bdml.bdwb))
            {
                errormsg += "标段文本不能为空;";
            }
            if (bdml.je == 0)
            {
                errormsg += "标段金额不能为0;";
            }
            if (string.IsNullOrEmpty(bdml.lrrid))
            {
                errormsg += "当前用户id不能为空;";
            }
            if (string.IsNullOrEmpty(bdml.lrr))
            {
                errormsg += "当前用户名不能为空;";
            }
            if (string.IsNullOrEmpty(bdml.lrrq))
            {
                errormsg += "录入日期不能为空;";
            }
            //if (string.IsNullOrEmpty(bdml.zt))
            //{
            //    errormsg += "审核状态不能为空;";
            //}
            if (string.IsNullOrEmpty(bdml.xmid))
            {
                errormsg += "项目id不能为空;";
            }
            if (errormsg != "")
            {
                errormsg = "采购标段目录中：" + errormsg;
            }

            for (int i = 0; i < mxs.Count; i++)
            {
                if (string.IsNullOrEmpty(mxs[i].cgpm))
                {
                    errormsgmx += "采购品目不能为空;";
                }
                if (string.IsNullOrEmpty(mxs[i].wpmc))
                {
                    errormsgmx += "物品名称不能为空;";
                }
                if (string.IsNullOrEmpty(mxs[i].jldw))
                {
                    errormsgmx += "计量单位不能为空;";
                }
                if (mxs[i].bdsl == 0)
                {
                    errormsgmx += "标段数量不能为0;";
                }
                if (mxs[i].bddj == 0)
                {
                    errormsgmx += "标段单价不能为0;";
                }
                if (mxs[i].bdje == 0)
                {
                    errormsgmx += "标段金额不能为0;";
                }
                if (string.IsNullOrEmpty(mxs[i].cgmxid))
                {
                    errormsgmx += "采购项目明细id不能为空;";
                }

            }
            if (errormsgmx != "")
            {
                errormsgmx = "采购标段明细中：" + errormsgmx;
            }
            if (errormsg + errormsgmx != "")
            {
                throw new Exception(errormsg + errormsgmx);
            }
           
        }


        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertBDXX(CG_BDXX bd)
        //{
        //    try
        //    {
        //        await SaveBDXX(bd);

        //        string bdmx = "";
        //        CG_BDXXML bdml = bd.cgbdml;
        //        List<CG_BDXXMX> mxs = bd.cgbdmx;

        //        if (string.IsNullOrEmpty(bdml.listid))
        //        {
        //            if (string.IsNullOrEmpty(bdml.kjnd))
        //            {
        //                bdml.kjnd = DateTime.Now.ToString("yyyy");
        //            }
        //            bdml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //            await BusinessRule.CGGL.InsertBDML(bdml);
        //        }
        //        else
        //        {
        //            await BusinessRule.CGGL.UpdateBDML(bdml);
        //        }
        //        foreach (CG_BDXXMX mx in bd.cgbdmx)
        //        {
        //            if (string.IsNullOrEmpty(mx.listid))
        //            {
        //                mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (string.IsNullOrEmpty(mx.bdid))
        //                {
        //                    mx.bdid = bdml.listid;
        //                }
        //                await BusinessRule.CGGL.InsertBDMX(mx);
        //                bdmx += "'" + mx.listid + "',";
        //            }
        //            else
        //            {
        //                await BusinessRule.CGGL.UpdateBDMX(mx);
        //                bdmx += "'" + mx.listid + "',";
        //            }
        //        }
        //        if (bdmx != "")
        //        {
        //            bdmx = "(" + bdmx.Substring(0, bdmx.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteBDNR(bdmx, bdml.listid);
        //        }

        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail(ex.Message);
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertBDXX(CG_BDXX bd)
        {
            try
            {
                string bdmx = "";
                CG_BDXXML bdml = bd.cgbdml;
                List<CG_BDXXMX> mxs = bd.cgbdmx;

                FileUploadent filecodes = bd.filescodes;


                string strope = bd.ope;

                if (strope != "add" && strope != "edit" && strope != "del" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

                await SaveBDXX(bd);

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(bdml.listid))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CG_BDXXML jhe = await TranstionRule.FindEntityByKey<CG_BDXXML>(bdml.listid);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    bdml.zt = "1";
                }

              

                if (string.IsNullOrEmpty(bdml.listid))
                {
                    if (string.IsNullOrEmpty(bdml.kjnd))
                    {
                        bdml.kjnd = DateTime.Now.ToString("yyyy");
                    }
                    bdml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    bdml.bddm = await JavaApi.BusinessBMJavaAPI(CGGLAudit.listTableName[TableName.采购标段.ToString()], bd.ToJson(), await GetICApiInfo());

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(bdml.listid, filecodes, await GetICApiInfo());

                    if (strlsope != "submit")
                        bdml.zt = "0";
                    if (strlsope == "submit")
                    {
                        //提交审批
                        //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = bdml.bddm, amount = bdml.je.ToString(), content = "[采购]" + bdml.bdmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购标段.ToString(), bdml.listid, filecodes.formCode, bd.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CGGL.InsertBDML(bdml);
                }
                else
                {
                    if (strlsope == "submit")
                    {
                        //提交审批
                        //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = bdml.bddm, amount = bdml.je.ToString(), content = "[采购]" + bdml.bdmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购标段.ToString(), bdml.listid, filecodes.formCode, bd.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CGGL.UpdateBDML(bdml);
                }

                foreach (CG_BDXXMX mx in bd.cgbdmx)
                {
                    if (string.IsNullOrEmpty(mx.listid))
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(mx.bdid))
                        {
                            mx.bdid = bdml.listid;
                        }
                        await BusinessRule.CGGL.InsertBDMX(mx);
                        bdmx += "'" + mx.listid + "',";
                    }
                    else
                    {
                        await BusinessRule.CGGL.UpdateBDMX(mx);
                        bdmx += "'" + mx.listid + "',";
                    }
                }
                if (bdmx != "")
                {
                    bdmx = "(" + bdmx.Substring(0, bdmx.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteBDNR(bdmx, bdml.listid);
                }

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGBDMLByID(String listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGBDMLByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGBDMXByID(String listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGBDMXByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteBDML([FromBody] Listid listid)
        {
            try
            {
                if (string.IsNullOrEmpty(listid.listid))
                {
                    return Fail("请发送对应标段的id");
                }
                else
                {
                    await BusinessRule.CGGL.DeleteBDML(listid.listid);

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SubmitBDML(CG_BDXX bd)//[FromBody] Listid listid
        {
            try
            {
                await SaveBDXX(bd);

                string bdmx = "";
                CG_BDXXML bdxxml = bd.cgbdml;
                List<CG_BDXXMX> mxs = bd.cgbdmx;

                FileUploadent filecodes = bd.filescodes;

                //这里先绑定附件
                await JavaApi.UploadFileJavaAPI(bdxxml.listid, filecodes, await GetICApiInfo());

                //提交审批
                //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                string urlView = "";
                string urlaudit = "";

                JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = bdxxml.bddm, amount = bdxxml.je.ToString(), content = "[采购]" + bdxxml.bdmc, deptName = GetUserDeptName() };
                JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购标段.ToString(), bdxxml.listid, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(bd.ToJson()), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                //if (!bsp)
                if (strInstanceId.StartsWith("err"))
                {
                    return Fail("提交失败，启动工作流异常");
                }

                bdxxml.zt = "1";

                if (string.IsNullOrEmpty(bdxxml.listid))
                {
                    if (string.IsNullOrEmpty(bdxxml.kjnd))
                    {
                        bdxxml.kjnd = DateTime.Now.ToString("yyyy");
                    }
                    bdxxml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    await BusinessRule.CGGL.InsertBDML(bdxxml);
                }
                else
                {
                    await BusinessRule.CGGL.UpdateBDML(bdxxml);
                }
                foreach (CG_BDXXMX mx in bd.cgbdmx)
                {
                    if (string.IsNullOrEmpty(mx.listid))
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(mx.bdid))
                        {
                            mx.bdid = bdxxml.listid;
                        }
                        await BusinessRule.CGGL.InsertBDMX(mx);
                        bdmx += "'" + mx.listid + "',";
                    }
                    else
                    {
                        await BusinessRule.CGGL.UpdateBDMX(mx);
                        bdmx += "'" + mx.listid + "',";
                    }
                }
                if (bdmx != "")
                {
                    bdmx = "(" + bdmx.Substring(0, bdmx.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteBDNR(bdmx, bdxxml.listid);
                }

                return Success("成功");
                //if (string.IsNullOrEmpty(listid.listid))
                //{
                //    return Fail("请发送对应标段的id");
                //}
                //else
                //{

                //    await BusinessRule.CGGL.SubmitBDML(listid.listid);

                //    return Success("成功");
                //}
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "招投标信息"
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetZBXX([FromQuery] ZBXXQuerry entity)
        {
            try
            {
                entity.UserID = GetUserId();
                (IEnumerable<dynamic>, int) SelectTable = await BusinessRule.CGGL.GetZBXX(entity, await GetDepId("procurement_permission"));

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.listid;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(entity.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.listid;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    string zt = item.zt.ToString();
                    item.spstatus = CGGLAudit.GetXMCGSPZTName(zt);

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetBDLB(string kjnd, int Size, int Current, string sortString, string sort)
        {
            try
            {

                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetBDLB(kjnd, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetBDbyids(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetBDbyids(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGBDMXBybdid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGBDMXBybdid(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCGBDMXByzbid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetCGBDMXByzbid(listid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZBByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetZBByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZBDWByID(String listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetZBDWByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        [HttpGet]
        public async Task SaveZBXX(CG_ZBXX cg)
        {
            string errormsg = "";
            string errormsgmx = "";
            string dwmx = "";
            CG_ZBXXML zbxx = cg.cgzbml;
            List<CG_ZBDWMX> mxs = cg.cgzbdw;
            if (string.IsNullOrEmpty(zbxx.bmdm))
            {
                errormsg += "部门代码不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.bmmc))
            {
                errormsg += "部门名称不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.bdmc))
            {
                errormsg += "标段名称不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.kbrq))
            {
                errormsg += "开标日期不能为空;";
            }
            if (zbxx.bdje == 0)
            {
                errormsg += "标段金额不能为0;";
            }
            if (string.IsNullOrEmpty(zbxx.cgbh))
            {
                errormsg += "采购编号不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.zbfzr))
            {
                errormsg += "招标负责人不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.ggnr))
            {
                errormsg += "公告内容不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.lrrid))
            {
                errormsg += "当前用户id不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.lrr))
            {
                errormsg += "当前用户名不能为空;";
            }
            if (string.IsNullOrEmpty(zbxx.lrrq))
            {
                errormsg += "录入日期不能为空;";
            }
            //if (string.IsNullOrEmpty(zbxx.zt))
            //{
            //    errormsg += "审核状态不能为空;";
            //}
            if (string.IsNullOrEmpty(zbxx.bdid))
            {
                errormsg += "标段id不能为空;";
            }
            if (errormsg != "")
            {
                errormsg = "招投标目录中：" + errormsg;
            }
            for (int i = 0; i < mxs.Count; i++)
            {
                if (string.IsNullOrEmpty(mxs[i].dwmc))
                {
                    errormsgmx += "单位名称不能为空;";
                }
                if (string.IsNullOrEmpty(mxs[i].zzjgdm))
                {
                    errormsgmx += "社会信用代码不能为空;";
                }
                if (string.IsNullOrEmpty(mxs[i].qylx))
                {
                    errormsgmx += "企业类型不能为空;";
                }
                if (string.IsNullOrEmpty(mxs[i].fddbr))
                {
                    errormsgmx += "法定代表人不能为空;";
                }
                if (mxs[i].zczb == 0)
                {
                    errormsgmx += "注册资本不能为0;";
                }
                if (string.IsNullOrEmpty(mxs[i].clsj))
                {
                    errormsgmx += "成立时间不能为空;";
                }
            }
            if (errormsgmx != "")
            {
                errormsgmx = "投标单位明细中：" + errormsgmx;
            }
            if (errormsg + errormsgmx != "")
            {
                throw new Exception(errormsg + errormsgmx);
            }
            //if (string.IsNullOrEmpty(zbxx.listid))
            //{
            //    if (string.IsNullOrEmpty(zbxx.kjnd))
            //    {
            //        zbxx.kjnd = DateTime.Now.ToString("yyyy");
            //    }
            //    zbxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //    if (await JavaApi.UploadFileJavaAPI(zbxx.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.InsertZBXXML(zbxx);
            //    }
            //}
            //else
            //{
            //    if (await JavaApi.UploadFileJavaAPI(zbxx.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.UpdateZBXXML(zbxx);
            //    }
            //}
            //foreach (CG_ZBDWMX mx in cg.cgzbdw)
            //{
            //    if (string.IsNullOrEmpty(mx.listid))
            //    {
            //        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (string.IsNullOrEmpty(mx.zbid))
            //        {
            //            mx.zbid = zbxx.listid;
            //        }
            //        await BusinessRule.CGGL.InsertZBDWMX(mx);
            //        dwmx += "'" + mx.listid + "',";
            //    }
            //    else
            //    {
            //        if (string.IsNullOrEmpty(mx.zbid))
            //        {
            //            mx.zbid = zbxx.listid;
            //        }
            //        await BusinessRule.CGGL.UpdateZBDWMX(mx);
            //        dwmx += "'" + mx.listid + "',";
            //    }
            //}
            //if (dwmx != "")
            //{
            //    dwmx = "(" + dwmx.Substring(0, dwmx.LastIndexOf(",")) + ")";
            //    await BusinessRule.CGGL.DeleteZBXMNR(dwmx, zbxx.listid);
            //}
        }

        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertZBXX(CG_ZBXX cg)
        //{
        //    try
        //    {
        //        await SaveZBXX(cg);
        //        string dwmx = "";
        //        CG_ZBXXML zbxx = cg.cgzbml;
        //        List<CG_ZBDWMX> mxs = cg.cgzbdw;

        //        if (string.IsNullOrEmpty(zbxx.listid))
        //        {
        //            if (string.IsNullOrEmpty(zbxx.kjnd))
        //            {
        //                zbxx.kjnd = DateTime.Now.ToString("yyyy");
        //            }
        //            zbxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //            if (await JavaApi.UploadFileJavaAPI(zbxx.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.InsertZBXXML(zbxx);
        //            }
        //        }
        //        else
        //        {
        //            if (await JavaApi.UploadFileJavaAPI(zbxx.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.UpdateZBXXML(zbxx);
        //            }
        //        }
        //        foreach (CG_ZBDWMX mx in cg.cgzbdw)
        //        {
        //            if (string.IsNullOrEmpty(mx.listid))
        //            {
        //                mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (string.IsNullOrEmpty(mx.zbid))
        //                {
        //                    mx.zbid = zbxx.listid;
        //                }
        //                await BusinessRule.CGGL.InsertZBDWMX(mx);
        //                dwmx += "'" + mx.listid + "',";
        //            }
        //            else
        //            {
        //                if (string.IsNullOrEmpty(mx.zbid))
        //                {
        //                    mx.zbid = zbxx.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateZBDWMX(mx);
        //                dwmx += "'" + mx.listid + "',";
        //            }
        //        }
        //        if (dwmx != "")
        //        {
        //            dwmx = "(" + dwmx.Substring(0, dwmx.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteZBXMNR(dwmx, zbxx.listid);
        //        }

        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail(ex.Message);
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertZBXX(CG_ZBXX cg)
        {
            try
            {
                await SaveZBXX(cg);

                string dwmx = "";
                CG_ZBXXML zbxx = cg.cgzbml;
                List<CG_ZBDWMX> mxs = cg.cgzbdw;

                FileUploadent filecodes = cg.filescodes;


                string strope = cg.ope;

                if (strope != "add" && strope != "edit" && strope != "del" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(zbxx.listid))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CG_ZBXXML jhe = await TranstionRule.FindEntityByKey<CG_ZBXXML>(zbxx.listid);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    zbxx.zt = "1";
                }

                await JavaApi.UploadFileJavaAPI(zbxx.listid, filecodes, await GetICApiInfo());

                if (string.IsNullOrEmpty(zbxx.listid))
                {
                    if (string.IsNullOrEmpty(zbxx.kjnd))
                    {
                        zbxx.kjnd = DateTime.Now.ToString("yyyy");
                    }
                    zbxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");

                    if (strlsope != "submit")
                        zbxx.zt = "0";

                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = zbxx.listid, amount = zbxx.bdje.ToString(), content = "[采购]" + zbxx.bdmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购招标.ToString(), zbxx.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CGGL.InsertZBXXML(zbxx);

                    //if (await JavaApi.UploadFileJavaAPI(zbxx.listid, cg.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.InsertZBXXML(zbxx);
                    //}
                }
                else
                {
                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = zbxx.listid, amount = zbxx.bdje.ToString(), content = "[采购]" + zbxx.bdmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购招标.ToString(), zbxx.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CGGL.UpdateZBXXML(zbxx);

                    //if (await JavaApi.UploadFileJavaAPI(zbxx.listid, cg.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.UpdateZBXXML(zbxx);
                    //}
                }
                foreach (CG_ZBDWMX mx in cg.cgzbdw)
                {
                    if (string.IsNullOrEmpty(mx.listid))
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(mx.zbid))
                        {
                            mx.zbid = zbxx.listid;
                        }
                        await BusinessRule.CGGL.InsertZBDWMX(mx);
                        dwmx += "'" + mx.listid + "',";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(mx.zbid))
                        {
                            mx.zbid = zbxx.listid;
                        }
                        await BusinessRule.CGGL.UpdateZBDWMX(mx);
                        dwmx += "'" + mx.listid + "',";
                    }
                }
                if (dwmx != "")
                {
                    dwmx = "(" + dwmx.Substring(0, dwmx.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteZBXMNR(dwmx, zbxx.listid);
                }

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteZBXX([FromBody] Listid listid)
        {
            try
            {
                if (string.IsNullOrEmpty(listid.listid))
                {
                    return Fail("请发送对应招投标的id");
                }
                else
                {
                    await BusinessRule.CGGL.DeleteZBXXML(listid.listid);

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SubmitZBXXML(CG_ZBXX zb)//[FromBody] CG_ZBXX cG_ZBXX
        {
            try
            {
                await SaveZBXX(zb);

                string dwmx = "";
                CG_ZBXXML zbxx = zb.cgzbml;
                FileUploadent filecodes = zb.filescodes;

          


                //这里先绑定附件
                await JavaApi.UploadFileJavaAPI(zbxx.listid, filecodes, await GetICApiInfo());

                //提交审批
                //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                string urlView = "";
                string urlaudit = "";

                JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = zbxx.listid, amount = zbxx.bdje.ToString(), content = "[采购]" + zbxx.bdmc, deptName = GetUserDeptName() };
                JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购招标.ToString(), zbxx.listid, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(zb.ToJson()), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                //if (!bsp)
                if (strInstanceId.StartsWith("err"))
                {
                    return Fail("提交失败，启动工作流异常");
                }

                //await BusinessRule.CGGL.SubmitZBXXML(cgzbml.listid);

                //提交
                zbxx.zt = "1";

                if (string.IsNullOrEmpty(zbxx.listid))
                {
                    if (string.IsNullOrEmpty(zbxx.kjnd))
                    {
                        zbxx.kjnd = DateTime.Now.ToString("yyyy");
                    }
                    zbxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    await BusinessRule.CGGL.InsertZBXXML(zbxx);
                }
                else
                {
                    await BusinessRule.CGGL.UpdateZBXXML(zbxx);
                }
                foreach (CG_ZBDWMX mx in zb.cgzbdw)
                {
                    if (string.IsNullOrEmpty(mx.listid))
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(mx.zbid))
                        {
                            mx.zbid = zbxx.listid;
                        }
                        await BusinessRule.CGGL.InsertZBDWMX(mx);
                        dwmx += "'" + mx.listid + "',";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(mx.zbid))
                        {
                            mx.zbid = zbxx.listid;
                        }
                        await BusinessRule.CGGL.UpdateZBDWMX(mx);
                        dwmx += "'" + mx.listid + "',";
                    }
                }
                if (dwmx != "")
                {
                    dwmx = "(" + dwmx.Substring(0, dwmx.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteZBXMNR(dwmx, zbxx.listid);
                }


                return Success("成功");

                //if (string.IsNullOrEmpty(cgzbml.listid))
                //{
                //    return Fail("请发送对应招投标的id");
                //}
                //else
                //{
                //    await BusinessRule.CGGL.SubmitZBXXML(listid.listid);

                //    return Success("成功");
                //}
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "评标中标信息"
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetPBZBXX([FromQuery] PBZBQuerry entity)
        {
            try
            {
                entity.UserID = GetUserId();
                (IEnumerable<dynamic>, int) SelectTable = await BusinessRule.CGGL.GetPBZBXX(entity, await GetDepId("procurement_permission"));

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.listid;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(entity.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.listid;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    string zt = item.zt.ToString();
                    item.spstatus = CGGLAudit.GetXMCGSPZTName(zt);

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetPBZBByID(String listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetPBZBByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetZBXXLB(string kjnd, int Size, int Current, string sortString, string sort)
        {
            try
            {
                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetZBXXLB(kjnd, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZBXXbyid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetZBXXbyid(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZBXXbypbzbid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetZBXXbypbzbid(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        [HttpGet]
        public async Task SavePBZB(CG_PBZBLB cg)
        {
            string errormsg = "";
            CG_PBZBML xm = cg.pbzb;
            if (string.IsNullOrEmpty(xm.bmdm))
            {
                errormsg += "部门代码不能为空;";
            }
            if (string.IsNullOrEmpty(xm.bmmc))
            {
                errormsg += "部门名称不能为空;";
            }
            if (string.IsNullOrEmpty(xm.bdmc))
            {
                errormsg += "标段名称不能为空;";
            }
            if (xm.bdje == 0)
            {
                errormsg += "标段金额不能为空;";
            }
            if (string.IsNullOrEmpty(xm.pbdw))
            {
                errormsg += "评标单位不能为空;";
            }
            if (string.IsNullOrEmpty(xm.pbsj))
            {
                errormsg += "评标时间不能为空;";
            }
            if (string.IsNullOrEmpty(xm.pbdd))
            {
                errormsg += "评标地点不能为空;";
            }
            if (string.IsNullOrEmpty(xm.pbfzr))
            {
                errormsg += "评标负责人不能为空;";
            }
            if (string.IsNullOrEmpty(xm.pw))
            {
                errormsg += "评委不能为空;";
            }
            if (string.IsNullOrEmpty(xm.zbdw))
            {
                errormsg += "中标单位不能为空;";
            }
            if (string.IsNullOrEmpty(xm.zbsj))
            {
                errormsg += "中标时间不能为空;";
            }
            if (string.IsNullOrEmpty(xm.zbdz))
            {
                errormsg += "中标单位地址不能为空;";
            }
            if (xm.zbje == 0)
            {
                errormsg += "中标金额不能为0;";
            }
            if (string.IsNullOrEmpty(xm.lrrid))
            {
                errormsg += "当前用户id不能为空;";
            }
            if (string.IsNullOrEmpty(xm.lrr))
            {
                errormsg += "当前用户名不能为空;";
            }
            if (string.IsNullOrEmpty(xm.lrrq))
            {
                errormsg += "录入日期不能为空;";
            }
            //if (string.IsNullOrEmpty(xm.zt))
            //{
            //    errormsg += "审核状态不能为空;";
            //}
            if (string.IsNullOrEmpty(xm.zbid))
            {
                errormsg += "招投标id不能为空;";
            }
            if (errormsg != "")
            {
                errormsg = "评标中标中：" + errormsg;
            }
            if (errormsg != "")
            {
                throw new Exception(errormsg);
            }
            if (string.IsNullOrEmpty(xm.kjnd))
            {
                xm.kjnd = DateTime.Now.ToString("yyyy");
            }
            //if (string.IsNullOrEmpty(xm.listid))
            //{
            //    xm.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //    if (await JavaApi.UploadFileJavaAPI(xm.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.InsertPBZB(xm);
            //    }
            //}
            //else
            //{
            //    if (await JavaApi.UploadFileJavaAPI(xm.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.UpdatePBZB(xm);
            //    }
            //}
        }

        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertPBZB(CG_PBZBLB zb)
        //{
        //    try
        //    {
        //        await SavePBZB(zb);

        //        CG_PBZBML xm = zb.pbzb;

        //        if (string.IsNullOrEmpty(xm.listid))
        //        {
        //            xm.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //            if (await JavaApi.UploadFileJavaAPI(xm.listid, zb.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.InsertPBZB(xm);
        //            }
        //        }
        //        else
        //        {
        //            if (await JavaApi.UploadFileJavaAPI(xm.listid, zb.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.UpdatePBZB(xm);
        //            }
        //        }

        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail(ex.Message);
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertPBZB(CG_PBZBLB zb)
        {
            try
            {
                await SavePBZB(zb);

                CG_PBZBML xm = zb.pbzb;
                FileUploadent filecodes = zb.filescodes;


                string strope = zb.ope;

                if (strope != "add" && strope != "edit" && strope != "del" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(xm.listid))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CG_PBZBML jhe = await TranstionRule.FindEntityByKey<CG_PBZBML>(xm.listid);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    xm.zt = "1";
                }

                await JavaApi.UploadFileJavaAPI(xm.listid, filecodes, await GetICApiInfo());
                if (string.IsNullOrEmpty(xm.listid))
                {
                    xm.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");

                    if (strlsope != "submit")
                        xm.zt = "0";

                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = xm.listid, amount = xm.bdje.ToString(), content = "[采购]" + xm.bdmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.评标中标.ToString(), xm.listid, filecodes.formCode, zb.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    await BusinessRule.CGGL.InsertPBZB(xm);
                    //if (await JavaApi.UploadFileJavaAPI(xm.listid, zb.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.InsertPBZB(xm);
                    //}
                }
                else
                {
                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = xm.listid, amount = xm.bdje.ToString(), content = "[采购]" + xm.bdmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.评标中标.ToString(), xm.listid, filecodes.formCode, zb.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    await BusinessRule.CGGL.UpdatePBZB(xm);
                    //if (await JavaApi.UploadFileJavaAPI(xm.listid, zb.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.UpdatePBZB(xm);
                    //}
                }

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeletePBZB([FromBody] Listid listid)
        {
            try
            {
                if (string.IsNullOrEmpty(listid.listid))
                {
                    return Fail("请发送对应投标中标的id");
                }
                else
                {
                    await BusinessRule.CGGL.DeletePBZB(listid.listid);

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SubmitPBZB(CG_PBZBLB zb)//[FromBody] Listid listid
        {
            try
            {
                await SavePBZB(zb);

                CG_PBZBML cgzbml = zb.pbzb;

                FileUploadent filecodes = zb.filescodes;


                //这里先绑定附件
                await JavaApi.UploadFileJavaAPI(cgzbml.listid, filecodes, await GetICApiInfo());

                //提交审批
                //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                string urlView = "";
                string urlaudit = "";

                JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgzbml.listid, amount = cgzbml.bdje.ToString(), content = "[采购]" + cgzbml.bdmc, deptName = GetUserDeptName() };
                JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.评标中标.ToString(), cgzbml.listid, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(zb.ToJson()), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                //if (!bsp)
                if (strInstanceId.StartsWith("err"))
                {
                    return Fail("提交失败，启动工作流异常");
                }

                cgzbml.zt = "1";
                if (string.IsNullOrEmpty(cgzbml.listid))
                {
                    cgzbml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    await BusinessRule.CGGL.InsertPBZB(cgzbml);
                }
                else
                {
                    await BusinessRule.CGGL.UpdatePBZB(cgzbml);
                }

                //await BusinessRule.CGGL.SubmitPBZB(cgzbml.listid);
                return Success("成功");
                //if (string.IsNullOrEmpty(listid.listid))
                //{
                //    return Fail("请发送对应投标中标的id");
                //}
                //else
                //{
                //    await BusinessRule.CGGL.SubmitPBZB(listid.listid);

                //    return Success("成功");
                //}
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion

        #region "合同和变更"
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetHTXX([FromQuery] HTXXQuerry entity)
        {
            try
            {
                entity.UserID = GetUserId();
                (IEnumerable<dynamic>, int) SelectTable = await BusinessRule.CGGL.GetHTXX(entity, await GetDepId("procurement_permission"));

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.listid;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(entity.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.listid;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    string zt = item.zt.ToString();
                    item.spstatus = CGGLAudit.GetXMCGSPZTName(zt);

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetPBZBLB(string kjnd, int Size, int Current, string sortString, string sort)
        {
            try
            {
                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetPBZBLB(kjnd, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetBDXXByPBID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetBDXXByPBID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        [HttpGet]
        public async Task SaveCGHT(CG_HT cg)
        {
            string errormsg = "";
            string errormsgskzh = "";
            string errormsgfkjh = "";
            string errormsgbzj = "";
            string htskzh = "";
            string htfkjh = "";
            string htbzj = "";
            CG_HTXX cgxmml = cg.cghtxx;
            List<CG_HTSKZH> skzhs = cg.cghtskzh;
            List<CG_HTFKJH> fkjhs = cg.cghtfkjh;
            List<CG_HTBZJ> bzjs = cg.cghtbzj;
            if (string.IsNullOrEmpty(cgxmml.sqr))
            {
                errormsg += "申请人不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.sqbm))
            {
                errormsg += "申请部门不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.sqrq))
            {
                errormsg += "申请日期不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jfmc))
            {
                errormsg += "甲方名称不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jfdb))
            {
                errormsg += "甲方法人代表不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jfry))
            {
                errormsg += "甲方代表人员不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jflxdh))
            {
                errormsg += "甲方联系电话不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jfdz))
            {
                errormsg += "甲方法定地址不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jfyb))
            {
                errormsg += "甲方邮编不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jfzzjgdm))
            {
                errormsg += "甲方社会信用代码不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jfsfz))
            {
                errormsg += "甲方身份证不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yfmc))
            {
                errormsg += "乙方名称不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yfdb))
            {
                errormsg += "乙方法人代表不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yfry))
            {
                errormsg += "乙方代表人员不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yflxdh))
            {
                errormsg += "乙方联系电话不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yfdz))
            {
                errormsg += "乙方法定地址不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yfyb))
            {
                errormsg += "乙方邮编不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yfzzjgdm))
            {
                errormsg += "乙方社会信用代码不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.yfsfz))
            {
                errormsg += "乙方身份证号不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bfmc))
            {
                errormsg += "丙方名称不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bfdb))
            {
                errormsg += "丙方法人代表不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bfry))
            {
                errormsg += "丙方代表人员不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bflxdh))
            {
                errormsg += "丙方联系电话不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bfdz))
            {
                errormsg += "丙方法定地址不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bfyb))
            {
                errormsg += "丙方邮编不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bfzzjgdm))
            {
                errormsg += "丙方社会信用代码不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.bfsfz))
            {
                errormsg += "丙方身份证号不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.htmc))
            {
                errormsg += "合同名称不能为空;";
            }
            //if (string.IsNullOrEmpty(cgxmml.htbh))
            //{
            //    errormsg += "合同编号不能为空;";
            //}
            if (string.IsNullOrEmpty(cgxmml.htqdrq))
            {
                errormsg += "合同签订日期不能为空;";
            }
            if (cgxmml.htje == 0)
            {
                errormsg += "合同金额不能为0;";
            }
            if (string.IsNullOrEmpty(cgxmml.htkssj))
            {
                errormsg += "合同开始时间不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.htjssj))
            {
                errormsg += "合同结束时间不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.jkfs))
            {
                errormsg += "交款方式不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.cfdd))
            {
                errormsg += "存放地点不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.htqddd))
            {
                errormsg += "合同签订地点不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.fkzytj))
            {
                errormsg += "付款主要条件不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.zyyw))
            {
                errormsg += "合同各方主要义务不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.wyzr))
            {
                errormsg += "违约责任不能为空;";
            }

            if (string.IsNullOrEmpty(cgxmml.lrrid))
            {
                errormsg += "当前用户id不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.lrr))
            {
                errormsg += "当前用户名不能为空;";
            }
            if (string.IsNullOrEmpty(cgxmml.lrrq))
            {
                errormsg += "录入日期不能为空;";
            }
            //if (string.IsNullOrEmpty(cgxmml.zt))
            //{
            //    errormsg += "审核状态不能为空;";
            //}
            if (string.IsNullOrEmpty(cgxmml.pbzbid))
            {
                errormsg += "评标中标id不能为空;";
            }
            if (errormsg != "")
            {
                errormsg = "采购合同中：" + errormsg;
            }

            for (int i = 0; i < skzhs.Count; i++)
            {
                if (string.IsNullOrEmpty(skzhs[i].gys))
                {
                    errormsgskzh += "供应商不能为空;";
                }
                if (string.IsNullOrEmpty(skzhs[i].nsrsbh))
                {
                    errormsgskzh += "纳税人实别号不能为空;";
                }
                if (string.IsNullOrEmpty(skzhs[i].khh))
                {
                    errormsgskzh += "开户行不能为空;";
                }
                if (string.IsNullOrEmpty(skzhs[i].skzh))
                {
                    errormsgskzh += "收款账号不能为空;";
                }
                if (string.IsNullOrEmpty(skzhs[i].lxr))
                {
                    errormsgskzh += "联系人不能为空;";
                }
                if (string.IsNullOrEmpty(skzhs[i].lxdh))
                {
                    errormsgskzh += "付款计划不能为空;";
                }
            }
            if (errormsgskzh != "")
            {
                errormsgskzh = "合同收款账号中：" + errormsgskzh;
            }
            for (int i = 0; i < fkjhs.Count; i++)
            {
                if (string.IsNullOrEmpty(fkjhs[i].jhnr))
                {
                    errormsgfkjh += "付款内容不能为空;";
                }
                if (string.IsNullOrEmpty(fkjhs[i].fkbl))
                {
                    errormsgfkjh += "付款比例不能为空;";
                }
                if (fkjhs[i].fkje == 0)
                {
                    errormsgfkjh += "付款金额不能为0;";
                }
                if (string.IsNullOrEmpty(fkjhs[i].fksj))
                {
                    errormsgfkjh += "付款时间不能为空;";
                }
                if (string.IsNullOrEmpty(fkjhs[i].fktj))
                {
                    errormsgfkjh += "付款条件不能为空;";
                }
            }
            if (errormsgfkjh != "")
            {
                errormsgfkjh = "合同付款计划中：" + errormsgfkjh;
            }
            for (int i = 0; i < bzjs.Count; i++)
            {
                if (bzjs[i].bzje == 0)
                {
                    errormsgbzj += "保证金金额不能为0;";
                }
                if (string.IsNullOrEmpty(bzjs[i].dzsj))
                {
                    errormsgbzj += "保证金到账时间不能为空;";
                }
                if (bzjs[i].thje == 0)
                {
                    errormsgbzj += "退回金额不能为0;";
                }
                if (string.IsNullOrEmpty(bzjs[i].thsj))
                {
                    errormsgbzj += "退回时间不能为空;";
                }
                if (string.IsNullOrEmpty(bzjs[i].sfth))
                {
                    errormsgbzj += "是否退回不能为空;";
                }
            }
            if (errormsgbzj != "")
            {
                errormsgbzj = "合同保证金中：" + errormsgbzj;
            }
            if (errormsg + errormsgfkjh + errormsgskzh + errormsgbzj != "")
            {
                throw new Exception(errormsg + errormsgfkjh + errormsgskzh + errormsgbzj);
            }
            //if (string.IsNullOrEmpty(cgxmml.kjnd))
            //{
            //    cgxmml.kjnd = DateTime.Now.ToString("yyyy");
            //}
            //if (cgxmml.listid == "")
            //{
            //    cgxmml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //    if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.InsertHTXX(cgxmml);
            //    }
            //}
            //else
            //{
            //    if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
            //    {
            //        await BusinessRule.CGGL.UpdateHTXX(cgxmml);
            //    }
            //}
            //foreach (CG_HTSKZH mx in cg.cghtskzh)
            //{
            //    if (mx.listid == "")
            //    {
            //        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (mx.htid == "")
            //        {
            //            mx.htid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.InsertSKZH(mx);
            //        htskzh += "'" + mx.listid + "',";
            //    }
            //    else
            //    {
            //        if (mx.htid == "")
            //        {
            //            mx.htid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.UpdateSKZH(mx);
            //        htskzh += "'" + mx.listid + "',";
            //    }
            //}
            //foreach (CG_HTFKJH jh in cg.cghtfkjh)
            //{
            //    if (jh.listid == "")
            //    {
            //        jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (jh.htid == "")
            //        {
            //            jh.htid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.InsertFKJH(jh);
            //        htfkjh += "'" + jh.listid + "',";
            //    }
            //    else
            //    {
            //        if (jh.htid == "")
            //        {
            //            jh.htid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.UpdateFKJH(jh);
            //        htfkjh += "'" + jh.listid + "',";
            //    }
            //}
            //foreach (CG_HTBZJ zb in cg.cghtbzj)
            //{
            //    if (zb.listid == "")
            //    {
            //        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (zb.htid == "")
            //        {
            //            zb.htid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.InsertBZJ(zb);
            //        htbzj += "'" + zb.listid + "',";
            //    }
            //    else
            //    {
            //        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            //        if (zb.htid == "")
            //        {
            //            zb.htid = cgxmml.listid;
            //        }
            //        await BusinessRule.CGGL.UpdateBZJ(zb);
            //        htbzj += "'" + zb.listid + "',";
            //    }
            //}
            //if (htskzh != "")
            //{
            //    htskzh = "(" + htskzh.Substring(0, htskzh.LastIndexOf(",")) + ")";
            //    await BusinessRule.CGGL.DeleteHTSKZH(htskzh, cgxmml.listid);
            //}
            //if (htfkjh != "")
            //{
            //    htfkjh = "(" + htfkjh.Substring(0, htfkjh.LastIndexOf(",")) + ")";
            //    await BusinessRule.CGGL.DeleteHTFKJH(htfkjh, cgxmml.listid);
            //}
            //if (htbzj != "")
            //{
            //    htbzj = "(" + htbzj.Substring(0, htbzj.LastIndexOf(",")) + ")";
            //    await BusinessRule.CGGL.DeleteHTBZJ(htbzj, cgxmml.listid);
            //}
        }

        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertHT(CG_HT cg)
        //{
        //    try
        //    {
        //        await SaveCGHT(cg);

        //        string htskzh = "";
        //        string htfkjh = "";
        //        string htbzj = "";
        //        CG_HTXX cgxmml = cg.cghtxx;
        //        List<CG_HTSKZH> skzhs = cg.cghtskzh;
        //        List<CG_HTFKJH> fkjhs = cg.cghtfkjh;
        //        List<CG_HTBZJ> bzjs = cg.cghtbzj;

        //        if (string.IsNullOrEmpty(cgxmml.kjnd))
        //        {
        //            cgxmml.kjnd = DateTime.Now.ToString("yyyy");
        //        }
        //        if (cgxmml.listid == "")
        //        {
        //            cgxmml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //            if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.InsertHTXX(cgxmml);
        //            }
        //        }
        //        else
        //        {
        //            if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.UpdateHTXX(cgxmml);
        //            }
        //        }
        //        foreach (CG_HTSKZH mx in cg.cghtskzh)
        //        {
        //            if (mx.listid == "")
        //            {
        //                mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (mx.htid == "")
        //                {
        //                    mx.htid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.InsertSKZH(mx);
        //                htskzh += "'" + mx.listid + "',";
        //            }
        //            else
        //            {
        //                if (mx.htid == "")
        //                {
        //                    mx.htid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateSKZH(mx);
        //                htskzh += "'" + mx.listid + "',";
        //            }
        //        }
        //        foreach (CG_HTFKJH jh in cg.cghtfkjh)
        //        {
        //            if (jh.listid == "")
        //            {
        //                jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (jh.htid == "")
        //                {
        //                    jh.htid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.InsertFKJH(jh);
        //                htfkjh += "'" + jh.listid + "',";
        //            }
        //            else
        //            {
        //                if (jh.htid == "")
        //                {
        //                    jh.htid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateFKJH(jh);
        //                htfkjh += "'" + jh.listid + "',";
        //            }
        //        }
        //        foreach (CG_HTBZJ zb in cg.cghtbzj)
        //        {
        //            if (zb.listid == "")
        //            {
        //                zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (zb.htid == "")
        //                {
        //                    zb.htid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.InsertBZJ(zb);
        //                htbzj += "'" + zb.listid + "',";
        //            }
        //            else
        //            {
        //                zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (zb.htid == "")
        //                {
        //                    zb.htid = cgxmml.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateBZJ(zb);
        //                htbzj += "'" + zb.listid + "',";
        //            }
        //        }
        //        if (htskzh != "")
        //        {
        //            htskzh = "(" + htskzh.Substring(0, htskzh.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteHTSKZH(htskzh, cgxmml.listid);
        //        }
        //        if (htfkjh != "")
        //        {
        //            htfkjh = "(" + htfkjh.Substring(0, htfkjh.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteHTFKJH(htfkjh, cgxmml.listid);
        //        }
        //        if (htbzj != "")
        //        {
        //            htbzj = "(" + htbzj.Substring(0, htbzj.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteHTBZJ(htbzj, cgxmml.listid);
        //        }

        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail(ex.Message);
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertHT(CG_HT cg)
        {
            try
            {
                await SaveCGHT(cg);

                string htskzh = "";
                string htfkjh = "";
                string htbzj = "";
                CG_HTXX cgxmml = cg.cghtxx;
                List<CG_HTSKZH> skzhs = cg.cghtskzh;
                List<CG_HTFKJH> fkjhs = cg.cghtfkjh;
                List<CG_HTBZJ> bzjs = cg.cghtbzj;

                FileUploadent filecodes = cg.filescodes;


                string strope = cg.ope;

                if (strope != "add" && strope != "edit" && strope != "del" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cgxmml.listid))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CG_HTXX jhe = await TranstionRule.FindEntityByKey<CG_HTXX>(cgxmml.listid);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cgxmml.zt = "1";
                }

                await JavaApi.UploadFileJavaAPI(cgxmml.listid, filecodes, await GetICApiInfo());

                if (string.IsNullOrEmpty(cgxmml.kjnd))
                {
                    cgxmml.kjnd = DateTime.Now.ToString("yyyy");
                }
                if (cgxmml.listid == "")
                {
                    cgxmml.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    //合同编号
                    cgxmml.htbh = await JavaApi.BusinessBMJavaAPI(CGGLAudit.listTableName[TableName.采购合同.ToString()], cg.ToJson(), await GetICApiInfo());

                    if (strlsope != "submit")
                        cgxmml.zt = "0";

                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgxmml.listid, amount = cgxmml.htje.ToString(), content = "[采购]" + cgxmml.htmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购合同.ToString(), cgxmml.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    await BusinessRule.CGGL.InsertHTXX(cgxmml);
                    //if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.InsertHTXX(cgxmml);
                    //}
                }
                else
                {
                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgxmml.listid, amount = cgxmml.htje.ToString(), content = "[采购]" + cgxmml.htmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购合同.ToString(), cgxmml.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    await BusinessRule.CGGL.UpdateHTXX(cgxmml);
                    //if (await JavaApi.UploadFileJavaAPI(cgxmml.listid, cg.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.UpdateHTXX(cgxmml);
                    //}
                }
                foreach (CG_HTSKZH mx in cg.cghtskzh)
                {
                    if (mx.listid == "")
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (mx.htid == "")
                        {
                            mx.htid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.InsertSKZH(mx);
                        htskzh += "'" + mx.listid + "',";
                    }
                    else
                    {
                        if (mx.htid == "")
                        {
                            mx.htid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.UpdateSKZH(mx);
                        htskzh += "'" + mx.listid + "',";
                    }
                }
                foreach (CG_HTFKJH jh in cg.cghtfkjh)
                {
                    if (jh.listid == "")
                    {
                        jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (jh.htid == "")
                        {
                            jh.htid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.InsertFKJH(jh);
                        htfkjh += "'" + jh.listid + "',";
                    }
                    else
                    {
                        if (jh.htid == "")
                        {
                            jh.htid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.UpdateFKJH(jh);
                        htfkjh += "'" + jh.listid + "',";
                    }
                }
                foreach (CG_HTBZJ zb in cg.cghtbzj)
                {
                    if (zb.listid == "")
                    {
                        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (zb.htid == "")
                        {
                            zb.htid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.InsertBZJ(zb);
                        htbzj += "'" + zb.listid + "',";
                    }
                    else
                    {
                        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (zb.htid == "")
                        {
                            zb.htid = cgxmml.listid;
                        }
                        await BusinessRule.CGGL.UpdateBZJ(zb);
                        htbzj += "'" + zb.listid + "',";
                    }
                }
                if (htskzh != "")
                {
                    htskzh = "(" + htskzh.Substring(0, htskzh.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteHTSKZH(htskzh, cgxmml.listid);
                }
                if (htfkjh != "")
                {
                    htfkjh = "(" + htfkjh.Substring(0, htfkjh.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteHTFKJH(htfkjh, cgxmml.listid);
                }
                if (htbzj != "")
                {
                    htbzj = "(" + htbzj.Substring(0, htbzj.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteHTBZJ(htbzj, cgxmml.listid);
                }

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetHTByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTSKZHByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetHTSKZHByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTFKJHByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetHTFKJHByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTBZJByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetHTBZJByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTBDXXByID(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetHTBDXXByID(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteHTXX([FromBody] Listid listid)
        {
            try
            {
                if (string.IsNullOrEmpty(listid.listid))
                {
                    return Fail("请发送对应合同的id");
                }
                else
                {
                    await BusinessRule.CGGL.DeleteHTXX(listid.listid);

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SubmitHTXX(CG_HT ht)//[FromBody] Listid listid
        {
            try
            {
                await SaveCGHT(ht);

                string htskzh = "";
                string htfkjh = "";
                string htbzj = "";
                CG_HTXX htxx = ht.cghtxx;
                List<CG_HTSKZH> skzhs = ht.cghtskzh;
                List<CG_HTFKJH> fkjhs = ht.cghtfkjh;
                List<CG_HTBZJ> bzjs = ht.cghtbzj;


                FileUploadent filecodes = ht.filescodes;



                //这里先绑定附件
                await JavaApi.UploadFileJavaAPI(htxx.listid, filecodes, await GetICApiInfo());

                //提交审批
                //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                string urlView = "";
                string urlaudit = "";

                JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = htxx.listid, amount = htxx.htje.ToString(), content = "[采购]" + htxx.htmc, deptName = GetUserDeptName() };
                JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购合同.ToString(), htxx.listid, filecodes.formCode, ht.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                //if (!bsp)
                if (strInstanceId.StartsWith("err"))
                {
                    return Fail("提交失败，启动工作流异常");
                }


                htxx.zt = "1";
                if (string.IsNullOrEmpty(htxx.kjnd))
                {
                    htxx.kjnd = DateTime.Now.ToString("yyyy");
                }
                if (htxx.listid == "")
                {
                    htxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    await BusinessRule.CGGL.InsertHTXX(htxx);
                }
                else
                {
                    await BusinessRule.CGGL.UpdateHTXX(htxx);
                }
                foreach (CG_HTSKZH mx in ht.cghtskzh)
                {
                    if (mx.listid == "")
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (mx.htid == "")
                        {
                            mx.htid = htxx.listid;
                        }
                        await BusinessRule.CGGL.InsertSKZH(mx);
                        htskzh += "'" + mx.listid + "',";
                    }
                    else
                    {
                        if (mx.htid == "")
                        {
                            mx.htid = htxx.listid;
                        }
                        await BusinessRule.CGGL.UpdateSKZH(mx);
                        htskzh += "'" + mx.listid + "',";
                    }
                }
                foreach (CG_HTFKJH jh in ht.cghtfkjh)
                {
                    if (jh.listid == "")
                    {
                        jh.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (jh.htid == "")
                        {
                            jh.htid = htxx.listid;
                        }
                        await BusinessRule.CGGL.InsertFKJH(jh);
                        htfkjh += "'" + jh.listid + "',";
                    }
                    else
                    {
                        if (jh.htid == "")
                        {
                            jh.htid = htxx.listid;
                        }
                        await BusinessRule.CGGL.UpdateFKJH(jh);
                        htfkjh += "'" + jh.listid + "',";
                    }
                }
                foreach (CG_HTBZJ zb in ht.cghtbzj)
                {
                    if (zb.listid == "")
                    {
                        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (zb.htid == "")
                        {
                            zb.htid = htxx.listid;
                        }
                        await BusinessRule.CGGL.InsertBZJ(zb);
                        htbzj += "'" + zb.listid + "',";
                    }
                    else
                    {
                        zb.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (zb.htid == "")
                        {
                            zb.htid = htxx.listid;
                        }
                        await BusinessRule.CGGL.UpdateBZJ(zb);
                        htbzj += "'" + zb.listid + "',";
                    }
                }
                if (htskzh != "")
                {
                    htskzh = "(" + htskzh.Substring(0, htskzh.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteHTSKZH(htskzh, htxx.listid);
                }
                if (htfkjh != "")
                {
                    htfkjh = "(" + htfkjh.Substring(0, htfkjh.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteHTFKJH(htfkjh, htxx.listid);
                }
                if (htbzj != "")
                {
                    htbzj = "(" + htbzj.Substring(0, htbzj.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteHTBZJ(htbzj, htxx.listid);
                }

                //await BusinessRule.CGGL.SubmitHTXX(htxx.listid);
                return Success("成功");

                //if (string.IsNullOrEmpty(listid.listid))
                //{
                //    return Fail("请发送对应合同的id");
                //}
                //else
                //{
                //    await BusinessRule.CGGL.SubmitHTXX(listid.listid);

                //    return Success("成功");
                //}
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion

        #region "项目验收"
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetXMYSXX([FromQuery] XMYSQuerry entity)
        {
            try
            {
                entity.UserID = GetUserId();
                (IEnumerable<dynamic>, int) SelectTable = await BusinessRule.CGGL.GetXMYSXX(entity, await GetDepId("procurement_permission"));

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.listid;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(entity.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.listid;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    string zt = item.zt.ToString();
                    item.spstatus = CGGLAudit.GetXMCGSPZTName(zt);

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetYSBDLB(string kjnd, int Size, int Current, string sortString, string sort)
        {
            try
            {
                (DataTable, int) SelectTable = await BusinessRule.CGGL.GetYSBDLB(kjnd, Size, Current, sortString, sort, await GetDepId("procurement_permission"));
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetYSBDBybdid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetYSBDBybdid(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetYSBDMXBybdid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetYSBDMXBybdid(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        //[HttpGet]
        //public async Task SaveYSXX(CG_XMYS cg)
        //{

        //}

        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertYSXX(CG_XMYS cg)
        //{
        //    try
        //    {
        //        //await SaveYSXX(cg);

        //        string ysmx = "";
        //        CG_XMYSML ysxx = cg.cgysml;
        //        if (string.IsNullOrEmpty(ysxx.listid))
        //        {
        //            ysxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //            if (await JavaApi.UploadFileJavaAPI(ysxx.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.InsertYSXXML(ysxx);
        //            }
        //        }
        //        else
        //        {
        //            if (await JavaApi.UploadFileJavaAPI(ysxx.listid, cg.filescodes, await GetICApiInfo()))
        //            {
        //                await BusinessRule.CGGL.UpdateYSXXML(ysxx);
        //            }
        //        }
        //        foreach (CG_XMYSMX mx in cg.cgysmx)
        //        {
        //            if (string.IsNullOrEmpty(mx.listid))
        //            {
        //                mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
        //                if (string.IsNullOrEmpty(mx.ysid))
        //                {
        //                    mx.ysid = ysxx.listid;
        //                }

        //                await BusinessRule.CGGL.InsertYSMX(mx);
        //                ysmx += "'" + mx.listid + "',";
        //            }
        //            else
        //            {
        //                if (string.IsNullOrEmpty(mx.ysid))
        //                {
        //                    mx.ysid = ysxx.listid;
        //                }
        //                await BusinessRule.CGGL.UpdateYSMX(mx);
        //                ysmx += "'" + mx.listid + "',";
        //            }
        //        }
        //        if (ysmx != "")
        //        {
        //            ysmx = "(" + ysmx.Substring(0, ysmx.LastIndexOf(",")) + ")";
        //            await BusinessRule.CGGL.DeleteZBXMNR(ysmx, ysxx.listid);
        //        }
        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail(ex.Message);
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertYSXX(CG_XMYS cg)
        {
            try
            {
                //await SaveYSXX(cg);

 
                if (string.IsNullOrEmpty(cg.cgysml.bmdm))
                {
                    return Fail("申请人不能为空");
                }
                if (string.IsNullOrEmpty(cg.cgysml.bdid))
                {
                    return Fail("标段id不能为空"); 
                }


                string ysmx = "";
                CG_XMYSML ysxx = cg.cgysml;

                FileUploadent filecodes = cg.filescodes;


                string strope = cg.ope;

                if (strope != "add" && strope != "edit" && strope != "del" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(ysxx.listid))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CG_XMYSML jhe = await TranstionRule.FindEntityByKey<CG_XMYSML>(ysxx.listid);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    ysxx.zt = "1";
                }

                await JavaApi.UploadFileJavaAPI(ysxx.listid, filecodes, await GetICApiInfo());

                if (string.IsNullOrEmpty(ysxx.listid))
                {
                    ysxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");

                    if (strlsope != "submit")
                        ysxx.zt = "0";
                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = ysxx.listid, amount = ysxx.ysje.ToString(), content = "[采购]" + ysxx.ysmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购竣工.ToString(), ysxx.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    await BusinessRule.CGGL.InsertYSXXML(ysxx);
                    //if (await JavaApi.UploadFileJavaAPI(ysxx.listid, cg.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.InsertYSXXML(ysxx);
                    //}
                }
                else
                {
                    if (strlsope == "submit")
                    {
                        string urlView = "";
                        string urlaudit = "";

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = ysxx.listid, amount = ysxx.ysje.ToString(), content = "[采购]" + ysxx.ysmc, deptName = GetUserDeptName() };
                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购竣工.ToString(), ysxx.listid, filecodes.formCode, cg.ToJson(), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    await BusinessRule.CGGL.UpdateYSXXML(ysxx);

                    //if (await JavaApi.UploadFileJavaAPI(ysxx.listid, cg.filescodes, await GetICApiInfo()))
                    //{
                    //    await BusinessRule.CGGL.UpdateYSXXML(ysxx);
                    //}
                }
                foreach (CG_XMYSMX mx in cg.cgysmx)
                {
                    if (string.IsNullOrEmpty(mx.listid))
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(mx.ysid))
                        {
                            mx.ysid = ysxx.listid;
                        }

                        await BusinessRule.CGGL.InsertYSMX(mx);
                        ysmx += "'" + mx.listid + "',";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(mx.ysid))
                        {
                            mx.ysid = ysxx.listid;
                        }
                        await BusinessRule.CGGL.UpdateYSMX(mx);
                        ysmx += "'" + mx.listid + "',";
                    }
                }
                if (ysmx != "")
                {
                    ysmx = "(" + ysmx.Substring(0, ysmx.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteZBXMNR(ysmx, ysxx.listid);
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetYSMLByYSid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetYSMLByYSid(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetYSMXByYSid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetYSMXByYSid(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetYSBDByYSid(string listid)
        {
            try
            {
                DataTable SelectTable = await BusinessRule.CGGL.GetYSBDByYSid(listid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteYSXX([FromBody] Listid listid)
        {
            try
            {
                if (string.IsNullOrEmpty(listid.listid))
                {
                    return Fail("请发送对应项目验收的id");
                }
                else
                {
                    await BusinessRule.CGGL.DeleteYSXX(listid.listid);

                    return Success("成功");
                }
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SubmitYSXX(CG_XMYS ht)//[FromBody] Listid listid
        {
            try
            {

                //await SaveYSXX(ht);
                string ysmx = "";
                CG_XMYSML ysxx = ht.cgysml;
                FileUploadent filecodes = ht.filescodes;


                //这里先绑定附件
                await JavaApi.UploadFileJavaAPI(ysxx.listid, filecodes, await GetICApiInfo());

                //提交审批
                //var url = BaseEditPower.GetSPDetailUrlS(TableName.采购计划.ToString(), cgjh.listid);
                string urlView = "";
                string urlaudit = "";

                JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = ysxx.listid, amount = ysxx.ysje.ToString(), content = "[采购]" + ysxx.ysmc, deptName = GetUserDeptName() };
                JavaApi.SPLEnt spl = new JavaApi.SPLEnt(TableName.采购竣工.ToString(), ysxx.listid, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(ht.ToJson()), "http://ic-xmzc/netapi/CGGL/UpdateStatus", urlaudit, urlView, _description);
                //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());

                //if (!bsp)
                if (strInstanceId.StartsWith("err"))
                {
                    return Fail("提交失败，启动工作流异常");
                }


                ysxx.zt = "1";
                if (string.IsNullOrEmpty(ysxx.listid))
                {
                    ysxx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                    await BusinessRule.CGGL.InsertYSXXML(ysxx);
                }
                else
                {
                    await BusinessRule.CGGL.UpdateYSXXML(ysxx);
                }
                foreach (CG_XMYSMX mx in ht.cgysmx)
                {
                    if (string.IsNullOrEmpty(mx.listid))
                    {
                        mx.listid = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                        if (string.IsNullOrEmpty(mx.ysid))
                        {
                            mx.ysid = ysxx.listid;
                        }

                        await BusinessRule.CGGL.InsertYSMX(mx);
                        ysmx += "'" + mx.listid + "',";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(mx.ysid))
                        {
                            mx.ysid = ysxx.listid;
                        }
                        await BusinessRule.CGGL.UpdateYSMX(mx);
                        ysmx += "'" + mx.listid + "',";
                    }
                }
                if (ysmx != "")
                {
                    ysmx = "(" + ysmx.Substring(0, ysmx.LastIndexOf(",")) + ")";
                    await BusinessRule.CGGL.DeleteZBXMNR(ysmx, ysxx.listid);
                }
                return Success("成功");
                //if (string.IsNullOrEmpty(listid.listid))
                //{
                //    return Fail("请发送对应项目验收的id");
                //}
                //else
                //{
                //    await BusinessRule.CGGL.SubmitYSXX(listid.listid);

                //    return Success("成功");
                //}
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion
    }
}
